/*
 * ListOfUsers.java
 *
 * Created on 23.11.2007, 21:31:06
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.possoft.client.gui.panel.nastavenispodnipanel.components;

import java.util.Vector;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import cz.possoft.client.gui.panel.nastavenispodnipanel.panel.NastaveniShowPanel;

/**
 *
 * @author mara
 */
public class ListOfUsers extends JList {

    // pole se seznamem useru
    private Vector vectorOfUsers;
    private JScrollPane scrollPane;

    // Flag ma hodnotu false, pokud se prave zmenil list useru. V tomto pirpade nebudeme disablovat
    // ani enablovat tlacitko na vyzvani
    private boolean panelChangedFlag;

    //{"one", "<html><body><font color=blue>twoo</font></body></html>", "three", "four"};
    public ListOfUsers(String connectedUsers) {
        vectorOfUsers = new Vector();
        panelChangedFlag = true;
        setUserList(connectedUsers, "", "");
        scrollPane = new JScrollPane(this);
        scrollPane.setAutoscrolls(false);
        addListSelectionListener(new MyListSelectionListener());
    }

    public JScrollPane getScrollPane() {
        return scrollPane;
    }

    // smaze cely list
    public void clear() {
        vectorOfUsers.clear();
        setListData(vectorOfUsers);
    }

    // v users je seznam uzivatel jak dosli ze serveru ve formatu CONNECTED. napr.
    // CONNECTED: aaa FREE, bbb OCCUPIED,
    // z tohoto listu je vytvoren vektor, ktery je pak nasetovan do listu.
    // klient a oponent maji svou vlastni barvu
    public void setUserList(String users, String clientName, String opponentName) {
        panelChangedFlag = false;
        Object value = getSelectedValue();
        vectorOfUsers.clear();
        int carkaIndex;
        String user;
        if ((users.length() > 11) && (users.indexOf("CONNECTED: ") == 0)) {
            users = users.substring(11);
            while ((users.length() > 0) && (users.indexOf(",") != -1)) {
                carkaIndex = users.indexOf(",");
                user = addHtmlTags(users.substring(0, carkaIndex).trim(), clientName, opponentName);
                vectorOfUsers.add(user);
                if (users.length() > carkaIndex) {
                    users = users.substring(carkaIndex + 1);
                } else {
                    users = "";
                }
            }
        }
        // nastaveni listu a obnoveni posledniho vybraneho indexu
        setListData(vectorOfUsers);
        panelChangedFlag = true;
        if (value != null) {
            setSelectedValue(value, true);
        }
    }

    // metoda prida k userovi html tagy, cimz mu urci spravnou barvu.
    // napr. pro vstup: franta FREE
    // metoda vraci: <HTML>....<font color=green>franta</font>.....
    // klient a oponent maji svou vlastni barvu
    private String addHtmlTags(String user, String clientName, String opponentName) {
        int lastSpace = user.lastIndexOf(" ");
        String flag = user.substring(lastSpace + 1);
        String userName = user.substring(0, lastSpace);
        String color = "";
        if ((userName.equals(clientName)) || (userName.equals(opponentName))) {
            color = "blue";
        } else if (flag.equals("FREE")) {
            color = "#009900";
        } else if (flag.equals("OCCUPIED")) {
            color = "red";
        }
        return "<HTML><BODY><FONT color=" + color + ">" + userName + "</FONT></BODY></HTML>";
    }

// listener zabezpecuje disablovani a enablovani tlacitka na vyzvy souperum.
    private class MyListSelectionListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent lse) {
            if (panelChangedFlag) {
                String selected = (String) getSelectedValue();
                NastaveniShowPanel.getVyzivaciButton().setEnabled(canBeVyzvan(selected));
            }
        }

        // z daneho stringu vyparsuje barvu a povoli vyzvani jen pokud je zelena
        private boolean canBeVyzvan(String selected) {
            if ((selected == null) || (NastaveniShowPanel.getConnectThread().getClientName().equals(""))) {
                return false;
            }
            if (((selected.indexOf("><FONT color=#009900>") != -1)) && (NastaveniShowPanel.getConnectThread().getOpponentName().equals(""))) {
                return true;
            }
            return false;
        }
    }



    //
}
